﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Text.RegularExpressions;


namespace Silver
{
    class StartThread
    {
        public void ThreadStart()
        {
            Thread th = new Thread(new ThreadStart(GetSilverData1));
            th.IsBackground = true;
            th.Start();

        }

        public  void GetSilverData()
        {
            while (true)
            {

                SilverData sourcedata=new SilverData();
                string html = sourcedata.RetriveData();
                SObject[] deserializedSObject = JsonConvert.DeserializeObject<SObject[]>(html);

              //  var time = new DateTime(1970, 01, 01).AddMilliseconds(deserializedSObject[0].message.revTime.time).AddMinutes(deserializedSObject[0].message.revTime.timezoneOffset * -1);

                if (SetUIData != null)
                {
                    Control controlObject = SetUIData.Target as Control;
                    //目标对象是个Control
                    if (controlObject != null)
                    {
                        controlObject.Invoke(SetUIData, deserializedSObject[0].message.sell.ToString(), deserializedSObject[0].message.buy.ToString());  
                    }
                    else
                    {
                        SetUIData(deserializedSObject[0].message.sell.ToString(), deserializedSObject[0].message.buy.ToString());
                    }
                }

                Thread.Sleep(5 * 1000);
            }

        }

        public void GetSilverData1()
        {
            while (true)
            {

                SilverData sourcedata = new SilverData();
                string html = sourcedata.RetriveData();
                
                string pattern="<(td)*?[^>]*>.*</td>";
                Regex reg = new Regex(pattern);
                string buy_price="";
                string sell_price="";
                string date="";

                foreach (Match match in reg.Matches(html))
                {
                    if(match.Value.Contains("白银"))
                    {
                        Match match_date = match.NextMatch().NextMatch();
                        date=sourcedata.Delete_tag(match_date.ToString());

                        Match match_buy = match_date.NextMatch();
                        buy_price=sourcedata.Delete_tag(match_buy.ToString());

                        Match match_sell = match_buy.NextMatch();
                        sell_price=sourcedata.Delete_tag(match_sell.ToString());



                    }
                }
                

                

                //SObject[] deserializedSObject = JsonConvert.DeserializeObject<SObject[]>(html);


                //  var time = new DateTime(1970, 01, 01).AddMilliseconds(deserializedSObject[0].message.revTime.time).AddMinutes(deserializedSObject[0].message.revTime.timezoneOffset * -1);

                if (SetUIData != null)
                {
                    Control controlObject = SetUIData.Target as Control;
                    //目标对象是个Control
                    if (controlObject != null)
                    {
                        controlObject.Invoke(SetUIData,sell_price , buy_price);
                    }
                    else
                    {
                        SetUIData(sell_price, buy_price);
                    }
                }

                Thread.Sleep(5 * 1000);
            }

        }
        public SetUI SetUIData;//定义一个共有的委托对象，并调用这个对象
    }

   
}
